<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>设置用户自定义的错误处理函数</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.set-error-handler" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">set_error_handler</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">set_error_handler</span> &mdash; <span class="dc-title">设置用户自定义的错误处理函数</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.set-error-handler-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>set_error_handler</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$error_handler</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$error_types</code><span class="initializer"> = E_ALL | E_STRICT</span></span>
  ] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>

  <p class="para rdfs-comment">
   设置用户的函数 (<code class="parameter">error_handler</code>) 来处理脚本中出现的错误。
  </p>
  <p class="para">
   本函数可以用你自己定义的方式来处理运行中的错误，
   例如，在应用程序中严重错误发生时，或者在特定条件下触发了一个错误(使用
   <span class="function"><a href="trigger_error.html" class="function">trigger_error()</a></span>)，你需要对数据/文件做清理回收。
  </p>
  <p class="para">
   重要的是要记住 <code class="parameter">error_types</code> 里指定的错误类型都会绕过 PHP 标准错误处理程序，
   除非回调函数返回了 <strong><code>FALSE</code></strong>。
   <span class="function"><a href="error_reporting.html" class="function">error_reporting()</a></span> 设置将不会起到作用而你的错误处理函数继续会被调用
   —— 不过你仍然可以获取 <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> 的当前值，并做适当处理。
   需要特别注意的是带 <a href="language.operators.errorcontrol.html" class="link">@ error-control
   operator</a> 前缀的语句发生错误时，这个值会是 0。
  </p>
  <p class="para">
   同时注意，在需要时你有责任使用 <span class="function"><a href="die.html" class="function">die()</a></span>。
   如果错误处理程序返回了，脚本将会继续执行发生错误的后一行。
  </p>
  <p class="para">
   以下级别的错误不能由用户定义的函数来处理：
   <strong><code>E_ERROR</code></strong>、 <strong><code>E_PARSE</code></strong>、
   <strong><code>E_CORE_ERROR</code></strong>、 <strong><code>E_CORE_WARNING</code></strong>、
   <strong><code>E_COMPILE_ERROR</code></strong>、
   <strong><code>E_COMPILE_WARNING</code></strong>，和在
   调用 <span class="function"><strong>set_error_handler()</strong></span> 函数所在文件中产生的大多数
   <strong><code>E_STRICT</code></strong>。
  </p>
  <p class="para">
   如果错误发生在脚本执行之前（比如文件上传时），将不会
   调用自定义的错误处理程序因为它尚未在那时注册。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.set-error-handler-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">error_handler</code></dt>

     <dd>

      <p class="para">
       以下格式的回调（callback）：
       可以传入 <strong><code>NULL</code></strong> 重置处理程序到默认状态。
       除了可以传入函数名，还可以传入引用对象和对象方法名的数组。
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">handler</span></span>
         ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$errno</code></span>
        , <span class="methodparam"><span class="type">string</span> <code class="parameter">$errstr</code></span>
        [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$errfile</code></span>
        [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$errline</code></span>
        [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$errcontext</code></span>
       ]]] ) : <span class="type">bool</span></div>

       <dl>

        
         <dt>
<code class="parameter">errno</code></dt>

         <dd>

          <span class="simpara">
           第一个参数 <code class="parameter">errno</code>，包含了错误的级别，是一个 integer。
          </span>
         </dd>

        
        
         <dt>
<code class="parameter">errstr</code></dt>

         <dd>

          <span class="simpara">
           第二个参数 <code class="parameter">errstr</code>，包含了错误的信息，是一个 string。
          </span>
         </dd>

        
        
         <dt>
<code class="parameter">errfile</code></dt>

         <dd>

          <span class="simpara">
           第三个参数是可选的，<code class="parameter">errfile</code>，
           包含了发生错误的文件名，是一个 string。
          </span>
         </dd>

        
        
         <dt>
<code class="parameter">errline</code></dt>

         <dd>

          <span class="simpara">
           第四个参数是一个可选项， <code class="parameter">errline</code>，
           包含了错误发生的行号，是一个 integer。
          </span>
         </dd>

        
        
         <dt>
<code class="parameter">errcontext</code></dt>

         <dd>

          <span class="simpara">
           第五个可选参数， <code class="parameter">errcontext</code>，
           是一个指向错误发生时活动符号表的 array。
           也就是说，<code class="parameter">errcontext</code>
           会包含错误触发处作用域内所有变量的数组。
           用户的错误处理程序不应该修改错误上下文（context）。
          </span>
          <div class="warning"><strong class="warning">Warning</strong>
           <p class="simpara">
            PHP 7.2.0 后此参数被<em class="emphasis">弃用</em>了。
            极其不建议依赖它。
           </p>
          </div>
         </dd>

        
       </dl>

      </p>
      <p class="para">
       如果函数返回 <strong><code>FALSE</code></strong>，标准错误处理处理程序将会继续调用。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">error_types</code></dt>

     <dd>

      <p class="para">
       就像<a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> 的 ini 设置能够控制错误的显示一样， 
       此参数能够用于屏蔽 <code class="parameter">error_handler</code> 的触发。
       如果没有该掩码，
       无论 <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> 是如何设置的，
       <code class="parameter">error_handler</code> 都会在每个错误发生时被调用。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.set-error-handler-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果之前有定义过错误处理程序，则返回该程序名称的 string；如果是内置的错误处理程序，则返回 <strong><code>NULL</code></strong>。
   如果你指定了一个无效的回调函数，同样会返回 <strong><code>NULL</code></strong>。
   如果之前的错误处理程序是一个类的方法，此函数会返回一个带类和方法名的索引数组(indexed array)。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.set-error-handler-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <code class="parameter">errcontext</code> 被废弃。
        使用此参数时会导致 <strong><code>E_DEPRECATED</code></strong> 提醒。
       </td>
      </tr>

      <tr>
       <td>5.5.0</td>
       <td>
        <code class="parameter">error_handler</code> 可接收 <strong><code>NULL</code></strong>。
       </td>
      </tr>

      <tr>
       <td>5.2.0</td>
       <td>
        错误处理器必须返回 <strong><code>FALSE</code></strong> 来显示
        <var class="varname"><var class="varname"><a href="reserved.variables.phperrormsg.html" class="classname">$php_errormsg</a></var></var>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.set-error-handler-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-464">
    <p><strong>Example #1 用 <span class="function"><strong>set_error_handler()</strong></span> 和 <span class="function"><a href="trigger_error.html" class="function">trigger_error()</a></span> 进行错误处理</strong></p>
    <div class="example-contents"><p>
     以下示例展示了通过触发错误并以用户自定义的程序来进行内部异常的处理。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;error&nbsp;handler&nbsp;function<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myErrorHandler</span><span style="color: #007700">(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errfile</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errline</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!(</span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">()&nbsp;&amp;&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;This&nbsp;error&nbsp;code&nbsp;is&nbsp;not&nbsp;included&nbsp;in&nbsp;error_reporting,&nbsp;so&nbsp;let&nbsp;it&nbsp;fall<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;through&nbsp;to&nbsp;the&nbsp;standard&nbsp;PHP&nbsp;error&nbsp;handler<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;ERROR&lt;/b&gt;&nbsp;[</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">]&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;&nbsp;Fatal&nbsp;error&nbsp;on&nbsp;line&nbsp;</span><span style="color: #0000BB">$errline</span><span style="color: #DD0000">&nbsp;in&nbsp;file&nbsp;</span><span style="color: #0000BB">$errfile</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">",&nbsp;PHP&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_VERSION&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"&nbsp;("&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_OS&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">")&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Aborting...&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;WARNING&lt;/b&gt;&nbsp;[</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">]&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;b&gt;My&nbsp;NOTICE&lt;/b&gt;&nbsp;[</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">]&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Unknown&nbsp;error&nbsp;type:&nbsp;[</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">]&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Don't&nbsp;execute&nbsp;PHP&nbsp;internal&nbsp;error&nbsp;handler&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;function&nbsp;to&nbsp;test&nbsp;the&nbsp;error&nbsp;handling<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)&nbsp;||&nbsp;</span><span style="color: #0000BB">$scale&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"log(x)&nbsp;for&nbsp;x&nbsp;&lt;=&nbsp;0&nbsp;is&nbsp;undefined,&nbsp;you&nbsp;used:&nbsp;scale&nbsp;=&nbsp;</span><span style="color: #0000BB">$scale</span><span style="color: #DD0000">"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$vect</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Incorrect&nbsp;input&nbsp;vector,&nbsp;array&nbsp;of&nbsp;values&nbsp;expected"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$temp&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach(</span><span style="color: #0000BB">$vect&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$pos&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_numeric</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Value&nbsp;at&nbsp;position&nbsp;</span><span style="color: #0000BB">$pos</span><span style="color: #DD0000">&nbsp;is&nbsp;not&nbsp;a&nbsp;number,&nbsp;using&nbsp;0&nbsp;(zero)"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_NOTICE</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$temp</span><span style="color: #007700">[</span><span style="color: #0000BB">$pos</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">log</span><span style="color: #007700">(</span><span style="color: #0000BB">$scale</span><span style="color: #007700">)&nbsp;*&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$temp</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;set&nbsp;to&nbsp;the&nbsp;user&nbsp;defined&nbsp;error&nbsp;handler<br /></span><span style="color: #0000BB">$old_error_handler&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">set_error_handler</span><span style="color: #007700">(</span><span style="color: #DD0000">"myErrorHandler"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;trigger&nbsp;some&nbsp;errors,&nbsp;first&nbsp;define&nbsp;a&nbsp;mixed&nbsp;array&nbsp;with&nbsp;a&nbsp;non-numeric&nbsp;item<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"vector&nbsp;a\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">43.3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">21.11</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;generate&nbsp;second&nbsp;array<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;b&nbsp;-&nbsp;a&nbsp;notice&nbsp;(b&nbsp;=&nbsp;log(PI)&nbsp;*&nbsp;a)\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;Value&nbsp;at&nbsp;position&nbsp;$pos&nbsp;is&nbsp;not&nbsp;a&nbsp;number,&nbsp;using&nbsp;0&nbsp;(zero)&nbsp;*/<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">M_PI</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;is&nbsp;trouble,&nbsp;we&nbsp;pass&nbsp;a&nbsp;string&nbsp;instead&nbsp;of&nbsp;an&nbsp;array<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;c&nbsp;-&nbsp;a&nbsp;warning\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;Incorrect&nbsp;input&nbsp;vector,&nbsp;array&nbsp;of&nbsp;values&nbsp;expected&nbsp;*/<br /></span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #DD0000">"not&nbsp;array"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2.3</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;NULL<br /><br />//&nbsp;this&nbsp;is&nbsp;a&nbsp;critical&nbsp;error,&nbsp;log&nbsp;of&nbsp;zero&nbsp;or&nbsp;negative&nbsp;number&nbsp;is&nbsp;undefined<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"----\nvector&nbsp;d&nbsp;-&nbsp;fatal&nbsp;error\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/*&nbsp;log(x)&nbsp;for&nbsp;x&nbsp;&lt;=&nbsp;0&nbsp;is&nbsp;undefined,&nbsp;you&nbsp;used:&nbsp;scale&nbsp;=&nbsp;$scale"&nbsp;*/<br /></span><span style="color: #0000BB">$d&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scale_by_log</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">2.5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;Never&nbsp;reached<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
vector a
Array
(
    [0] =&gt; 2
    [1] =&gt; 3
    [2] =&gt; foo
    [3] =&gt; 5.5
    [4] =&gt; 43.3
    [5] =&gt; 21.11
)
----
vector b - a notice (b = log(PI) * a)
&lt;b&gt;My NOTICE&lt;/b&gt; [1024] Value at position 2 is not a number, using 0 (zero)&lt;br /&gt;
Array
(
    [0] =&gt; 2.2894597716988
    [1] =&gt; 3.4341896575482
    [2] =&gt; 0
    [3] =&gt; 6.2960143721717
    [4] =&gt; 49.566804057279
    [5] =&gt; 24.165247890281
)
----
vector c - a warning
&lt;b&gt;My WARNING&lt;/b&gt; [512] Incorrect input vector, array of values expected&lt;br /&gt;
NULL
----
vector d - fatal error
&lt;b&gt;My ERROR&lt;/b&gt; [256] log(x) for x &lt;= 0 is undefined, you used: scale = -2.5&lt;br /&gt;
  Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)&lt;br /&gt;
Aborting...&lt;br /&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.set-error-handler-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="class.errorexception.html" class="classname">ErrorException</a></li>
    <li class="member"><span class="function"><a href="error_reporting.html" class="function" rel="rdfs-seeAlso">error_reporting()</a> - 设置应该报告何种 PHP 错误</span></li>
    <li class="member"><span class="function"><a href="restore_error_handler.html" class="function" rel="rdfs-seeAlso">restore_error_handler()</a> - 还原之前的错误处理函数</span></li>
    <li class="member"><span class="function"><a href="trigger_error.html" class="function" rel="rdfs-seeAlso">trigger_error()</a> - 产生一个用户级别的 error/warning/notice 信息</span></li>
    <li class="member"><a href="errorfunc.constants.html" class="link">error level constants</a></li>
    <li class="member"><a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> 类型的信息</li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>